package com.example.wang7.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.wang7.R;
import com.example.wang7.ui1.biye.biyeFragment;
import com.example.wang7.ui1.xiuxue.xiuxueFragment;
import com.example.wang7.ui1.zaixiao.zaixiaoFragment;
import com.example.wang7.ui1.miaoxing.miaoxingFragment;
import com.google.android.material.tabs.TabLayout;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private zaixiaoFragment zaixiaoFragment;
    private biyeFragment biyeFragment;
    private xiuxueFragment xiuxueFragment;
    private miaoxingFragment miaoxingFragment;



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        biyeFragment = biyeFragment.newInstance();
        zaixiaoFragment = zaixiaoFragment.newInstance();
        xiuxueFragment = xiuxueFragment.newInstance();
       miaoxingFragment = miaoxingFragment.newInstance();
        getChildFragmentManager().beginTransaction().replace(R.id.linearLayout,zaixiaoFragment).commit();

        TabLayout tabLayout = (TabLayout) root.findViewById(R.id.tablayout);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //Toast.makeText(getActivity(),tab.getText(),Toast.LENGTH_SHORT).show();
                if (tab.getText().equals("毕业")){
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout,biyeFragment).commit();
                }
                if (tab.getText().equals("在校")){
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout,zaixiaoFragment).commit();}
                if (tab.getText().equals("休学")){
                        getChildFragmentManager().beginTransaction().replace(R.id.linearLayout,xiuxueFragment).commit();}
                if (tab.getText().equals("喵星")){
                            getChildFragmentManager().beginTransaction().replace(R.id.linearLayout,miaoxingFragment).commit();

            }}

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        return root;
    }
}